package com.zsh.adapters;

import java.util.List;

import com.zsh.activities.MainActivity;
import com.zsh.constant.Constant;
import com.zsh.prototype.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class FeaturesPagerAdapter extends PagerAdapter {
	private List<View> mList;
	private Activity mActivity;
	
	public FeaturesPagerAdapter(List<View> mList,Activity activity) {
		this.mList = mList;
		this.mActivity = activity;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0==arg1;  
	}
	
	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager)container).removeView(mList.get(position));
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
		Log.d("FeaturePagerAdapter", String.valueOf(position));
		((ViewPager)container).addView(mList.get(position));
		if (position == mList.size() - 1) {
			Button mStartImageButton = (Button) container.findViewById(R.id.iv_start);
			mStartImageButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					disableFirst();
					goHome();
				}

			});
		}
		return mList.get(position);
	}
	
	private void goHome() {
		Intent intent = new Intent(mActivity, MainActivity.class);
		mActivity.startActivity(intent);
		mActivity.finish();
	}

	private void disableFirst() {
		SharedPreferences preferences = mActivity.getSharedPreferences(
				Constant.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putBoolean("isFirstIn", false);
		editor.commit();
	}
}
